<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\models\Linkers;

class LinkerController extends BaseController
{
    /**
     * 获取商品列表
     * @return \think\response\Json
     */
    public function listsAction()
    {
        try{
            $linkers = Linkers::select();

            if (count($linkers)) {
                foreach ($linkers as $linker) {
                    $data[] = [
                        'linkerId' => $linker->linkerId,
                        'linkerName' => $linker->linkerName,
                        'linkerAppId' => $linker->linkerAppId,
                        'linkerPath' => $linker->linkerPath,
                    ];
                }
            }

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 详情
     * @param  [type] $id
     *
     * @return void
     */
    public function detailAction($id)
    {
        try{
            $linker = $this->app->linkerService->getDetail($id);
            if(!$linker){
                throw new \Exception("链接不存在",500);
            }
            $data = $linker->toArray();

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * 添加商品
     * @return \think\response\Json
     */
    public function addAction()
    {
        try{
            if(empty($this->post)){
                throw new \Exception("没有数据提交，不能处理",500);
            }
            $this->post['adminId'] = $this->admin->adminId;
            $this->post['appletId'] = $this->appletId;
            $this->app->linkerService->add($this->post);

            return json(['code' => 200, 'msg' => '添加成功', 'data'=> []]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 名录修改
     *
     * @return void
     */
    public function updateAction($id)
    {
        try {
            $linker = $this->app->linkerService->getDetail($id);

            $this->app->linkerService->update($linker, $this->post);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function deleteAction($id)
    {
        try {
            $linker = $this->app->linkerService->getDetail($id);

            $this->app->linkerService->delete($linker);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function enableAction($id)
    {
        try {
            $linker = $this->app->linkerService->getDetail($id);

            $this->app->linkerService->enable($linker);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function disableAction($id)
    {
        try {
            $linker = $this->app->linkerService->getDetail($id);

            $this->app->linkerService->disable($linker);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
}